package com.zxl.manager.dao.impl;

import com.zxl.manager.dao.OrderDao;
import com.zxl.manager.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jimmy on 2017/7/16.
 */
@Repository
public class OrderDaoImpl implements OrderDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Order> getAllOrder(Integer limit,Integer offset){

        String sql = "select a.ord_id,b.nick_name,d.product_name,e.`value`,a.prod_sum,a.prod_price,a.`status`,a.create_time,a.upd_time from wh_order a,wx_user b,product d,product_detail e where a.user_id = b.open_id and a.prod_id = d.product_id" +
                " and a.spec_id = e.detail_id order by a.create_time desc limit ?,?";
        List<Order> ordlist = jdbcTemplate.query(sql,new Object[]{offset,limit}, new RowMapper<Order>() {

            @Override
            public Order mapRow(ResultSet resultSet, int i) throws SQLException {
                Order order = new Order();
                order.setOrdId(resultSet.getLong(1));
                order.setUserName(resultSet.getString(2));
                order.setProdName(resultSet.getString(3));
                order.setSpec(resultSet.getString(4));
                order.setProdSum(resultSet.getString(5));
                order.setProdPrice(resultSet.getDouble(6));
                order.setType(resultSet.getString(7));
                order.setCreateTime(resultSet.getTimestamp(8));
                order.setUpdTime(resultSet.getTimestamp(9));
                return order;
            }
        });
        return ordlist;
    }

    public int totalOrder(){
        String sql = "select count(1) from wh_order a,wx_user b,product d,product_detail e where a.user_id = b.open_id and a.prod_id = d.product_id" +
                " and a.spec_id = e.detail_id order by a.create_time";
        Integer total = jdbcTemplate.query(sql,new RowMapper<Integer>(){

            @Override
            public Integer mapRow(ResultSet resultSet, int i) throws SQLException {
                Integer total = resultSet.getInt(1);
                return total;
            }
        }).get(0);
        return total;
    }

    public int updOrder(String ids){
        String sql = "update wh_order set status = 4 where ord_id in ("+ids+")";
        return jdbcTemplate.update(sql);
    }
}
